package com.itheima.service.impl;

import com.itheima.domain.Employee;
import com.itheima.mapper.EmployeeMapper;
import com.itheima.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author itheima
 * @since 2022-05-03
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<Employee> getEmployeeList(String name, Integer page, Integer pageSize) {
        // page = 2
        // pageSize = 10

        Integer offset = (page - 1) * pageSize;
        // LIMIT 起始值，查询长度
        // LIMIT 0, 10 =>   1 ~ 10
        // LIMIT 10, 10 => 11 ~20
        // LIMIT 20, 10 =>

        // page = 1  (page - 1) * pageSize = 0
        // page = 2  =10
        // page = 3 20

        // limit offset, pageSize
        // 从第几条数据开始查起

        // 假设：获取第一页数据，每页显示10条
        // 从第一条开始查，一查到10
        // offset 0~ 10
        // offset 10~20
        // offset 20~30

        // (page - 1) * pageSize
        // offset 0
        // offset 10
        // offset 20

        // PageHelper.startPage(1, 10);

        return employeeMapper.selectAll(name, offset, pageSize);
    }

    @Override
    public Long getEmployeeAmount(String name) {
        return employeeMapper.selectCount(name);
    }

    @Override
    public Employee getEmployeeById(String id) {
        return employeeMapper.selectById(id);
    }
}
